package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.entity.UsersAuthentication;
import com.serkol.ams.entity.UsersWalletLog;
import com.serkol.ams.entity.UsersWalletTransfer;
import com.serkol.ams.mapper.UsersWalletTransferMapper;
import com.serkol.ams.service.UsersAuthenticationService;
import com.serkol.ams.service.UsersWalletService;
import com.serkol.ams.service.UsersWalletTransferService;
import com.serkol.ams.service.UsersWalletWithdrawalService;
import com.serkol.constant.ResultCode;
import com.serkol.exception.AuthException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author pingan
 * @description 针对表【users_wallet_transfer】的数据库操作Service实现
 * @createDate 2022-11-29 22:20:24
 */
@Service
public class UsersWalletTransferServiceImpl extends ServiceImpl<UsersWalletTransferMapper, UsersWalletTransfer>
        implements UsersWalletTransferService {
    @Autowired
    private UsersWalletWithdrawalService walletWithdrawalService;


    @Autowired
    private UsersWalletService walletService;
    @Autowired
    private UsersAuthenticationService usersAuthenticationService;
    @Transactional
    @Override
    public boolean myReChange(UsersWalletTransfer walletTransfer) throws AuthException {

        UsersWalletLog walletLog1 = new UsersWalletLog();
        walletLog1.setUserId(walletTransfer.getUserId());
        walletLog1.setOperatorId(walletTransfer.getOperatorId());
        walletLog1.setPrice(walletTransfer.getPrice());
        walletLog1.setType(walletTransfer.getType());
        walletLog1.setRemark(walletTransfer.getRemark());
        walletLog1.setState(2);

        boolean b = walletService.reNoChange(walletLog1);
        if (b) {
            UsersAuthentication info = usersAuthenticationService.getInfo();
            UsersWalletLog walletLog2 = new UsersWalletLog();
            walletLog2.setUserId(walletTransfer.getToUserId());
            walletLog2.setOperatorId(walletTransfer.getOperatorId());
            walletLog2.setPrice(walletTransfer.getPrice());
            walletLog2.setType(walletTransfer.getType());
            walletLog2.setRemark(info.getName()+ "转账了");
            walletLog1.setState(1);
            boolean c = walletService.reChange(walletLog2);
            if (c) {
                boolean save = this.save(walletTransfer);
                if (save) {
                    return true;
                } else {
                    throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
                }

            } else {
                throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
            }

        } else {
            throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
        }

    }


}




